package com.ktjy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktjy.config.CustoRealm;
import com.ktjy.entity.SysRole;
import com.ktjy.entity.SysRoleRight;
import com.ktjy.service.ISysRightService;
import com.ktjy.service.ISysRoleRightService;
import com.ktjy.service.ISysRoleService;
import com.ktjy.utils.PageUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.realm.Realm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 控制器
 * </p>
 *
 * @author liuqiaomiao
 * @ClassName SysRoleController
 * @since 2024-03-05
 */
@Controller
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysRightService sysRightService;
    @Autowired
    private ISysRoleRightService roleRightService;

    /**
     * 跳转新增页面
     */
    @RequestMapping(value = "/add")
    public Object toInsert() {
        return "/role/add";
    }

    /**
     * 增
     */
    @PostMapping(value = "/save")
    @Transactional
    public Object insert(SysRole sysRole, String[] rightCodes) {

        if (null != sysRole.getRoleId()) {
            if (sysRoleService.updateById(sysRole)) {
                int deleteRow = roleRightService.removeRoleRight(sysRole.getRoleId());
                List<SysRoleRight>  addList=new ArrayList<>();
                for (int i = 0; i < rightCodes.length; i++) {
                    SysRoleRight sysRoleRight =new SysRoleRight();
                    sysRoleRight.setRfRightCode(rightCodes[i]);
                    sysRoleRight.setRfRoleId(sysRole.getRoleId());
                    addList.add(sysRoleRight);
                }
                roleRightService.saveOrUpdateBatch(addList);;

            }


           RealmSecurityManager realmSecurityManager= (RealmSecurityManager) SecurityUtils.getSecurityManager();
          CustoRealm realm= (CustoRealm) realmSecurityManager.getRealms().iterator().next();
//            realm.c

        } else {
            sysRoleService.save(sysRole);

            List<SysRoleRight>  addList=new ArrayList<>();
            for (int i = 0; i < rightCodes.length; i++) {
                SysRoleRight sysRoleRight =new SysRoleRight();
                sysRoleRight.setRfRightCode(rightCodes[i]);
                sysRoleRight.setRfRoleId(sysRole.getRoleId());
                addList.add(sysRoleRight);
            }
            roleRightService.saveOrUpdateBatch(addList);;

        }
        return "redirect:/role/list";


    }

    /**
     * 删
     */
    @RequestMapping(value = "/del/{id}")
    @ResponseBody
    public Object delete(@PathVariable(name = "id") Long id) {
        return sysRoleService.removeById(id);
    }

    /**
     * 跳转修改页面
     */
//
    @RequestMapping(value = "/edit")
    public Object toUpdate(Integer roleId, Model model) {


        model.addAttribute("role", sysRoleService.getById(roleId));
        model.addAttribute("rights", sysRightService.list());
        List<String> roleRights = roleRightService.getRightByUserRoleId(Long.valueOf(roleId)).stream()
                .map(sysRight -> {
                    return sysRight.getRightCode();
                }).collect(Collectors.toList());
        model.addAttribute("roleRights", roleRights);
        return "/role/edit";
    }


    /**
     * 分页列表
     */
    @RequestMapping(value = "/list")
    public Object getUserPage(PageUtil page, SysRole sysRole, Model model) {

        PageUtil<SysRole> page1 = (PageUtil<SysRole>) sysRoleService.page(page, new QueryWrapper<>(sysRole));

//        PageUtil pageUtil = new PageUtil(page1);

        model.addAttribute("rolePage", page1);
        return "/role/list";
    }

//    @RequestMapping("/list")
//    public String list(@RequestParam(defaultValue = "1") int PageIndex,
//                       @RequestParam(defaultValue = "5") int PageSize,
//                       Model model){
//        //查询分页数据
//
//        Page page =new Page();
//        page.setCurrent(PageIndex);
//        page.setSize(PageSize);
//        page= sysRoleService.page(page);
//
//
//        model.addAttribute("rolePage",page);
//
//        return "role/list";
//
//    }

}
